package uy.com.tsi2G7.superbet.event;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.ObjectNotFoundException;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;

import uy.com.tsi2G7.superbet.event.dao.EventStore;
import uy.com.tsi2G7.superbet.event.dao.ObjectAlreadyExistsException;
import uy.com.tsi2G7.superbet.util.SpringContext;


@ManagedBean(name = "eventService")
@Stateless
public class EventServiceImpl implements EventService {

	private EventStore eventStore;
	
	private EventStore getEventStore() {
		if(eventStore==null)
			eventStore=(EventStore) SpringContext.getApplicationContext().getBean("eventStore");
		return eventStore;
	}

	@Override
	public Map<EventMarket, List<EventMarketResult>> getCurrentMarkets(
			Long eventId, Long eventTypeId) {
		List<EventMarket> markets=getEventStore().getCurrentMarketsByEventId(eventId,eventTypeId);
		Map<EventMarket, List<EventMarketResult>> res=new HashMap<EventMarket, List<EventMarketResult>>();
		for (EventMarket eventMarket : markets) {
			List<EventMarketResult> results=getEventStore().getMarketsWithPendingResults(eventMarket.getMarketId());
			if(results!=null&&!results.isEmpty())
				res.put(eventMarket,results);
			else
				res.put(eventMarket,new ArrayList<EventMarketResult>());
		}
		return res;
	}

	@Override
	public List<EventMarketResult> getMarketResults(long eventTypeId,
			long marketId) {
		return getEventStore().getMarketsWithPendingResults(marketId);
	}

	@Override
	public EventMarket getMarketById(long marketId) {
		return getEventStore().getMarketById(marketId);
	}

	@Override
	public void addMarketResult(long marketResultId, String marketResultName,
			long eventTypeId, long eventId, long marketId, BigDecimal amount) throws ObjectNotFoundException, ObjectAlreadyExistsException {
		EventMarket market = getEventStore().getMarketById(marketId);
		if(market==null||market.getEventId()!=eventId||market.getEventTypeId()!=eventTypeId)
			throw new ObjectNotFoundException();
		getEventStore().storeMarketResult(marketResultId,marketResultName,marketId,eventId,eventTypeId,amount);
	}

	@Override
	public void addEvent(Long eventTypeId, long eventId, String eventInputName)
			throws ObjectNotFoundException, ObjectAlreadyExistsException {
		EventType eventType = getEventStore().getEventTypeById(eventTypeId);
		if(eventType==null)
			throw new ObjectNotFoundException();
		getEventStore().storeEvent(eventTypeId,eventId,eventInputName);
		
	}

	@Override
	public void addMarket(Long eventTypeId, Long eventId, String marketInputName,
			long marketId, Date marketDate) throws ObjectNotFoundException, ObjectAlreadyExistsException {
		Event event = getEventStore().getEventById(eventId);
		if(event==null||event.getId().longValue()!=eventId.longValue()||event.getEventTypeId().longValue()!=eventTypeId.longValue())
			throw new ObjectNotFoundException();
		getEventStore().storeMarket(eventId,eventTypeId,marketId,marketInputName,marketDate);
		
	}

	@Override
	public List<EventMarketResult> getMarketResultsById(List<Long> ids) {
		return getEventStore().getMarketResultsById(ids);
	}

	@Override
	public Map<EventMarket, List<EventMarketResult>> getMarketsWithPendingResults() {
		List<EventMarket> markets=getEventStore().getManualSourceMarketsWithPendingResults();
		Map<EventMarket, List<EventMarketResult>> res=new HashMap<EventMarket, List<EventMarketResult>>();
		for (EventMarket eventMarket : markets) {
			List<EventMarketResult> results=getEventStore().getMarketsWithPendingResults(eventMarket.getMarketId());
			if(results!=null&&!results.isEmpty())
				res.put(eventMarket,results);
			else
				res.put(eventMarket,new ArrayList<EventMarketResult>());
		}
		return res;
	}

	@Override
	public void setWinner(EventMarket market, long resultId) {
		List<EventMarketResult> results = getMarketResults(market.getEventTypeId(), market.getMarketId());
		for (EventMarketResult eventMarketResult : results) {
			if(eventMarketResult.getId()==resultId)
				eventMarketResult.setWinAmount(eventMarketResult.getAmount());
			else
				eventMarketResult.setWinAmount(BigDecimal.ZERO);
			getEventStore().mergeResult(eventMarketResult);
		}
	}

	@Override
	public List<Event> getAllEvents() {
		return getEventStore().getAllEvents();
	}

	@Override
	public List<EventType> getAllEventTypes() {
		return getEventStore().getAllEventTypes();
	}

	@Override
	public List<EventMarket> getCurrentMarketsByEventId(Long eventId,
			Long eventTypeId) {
		return getEventStore().getCurrentMarketsByEventId(eventId, eventTypeId);
	}
	
	
}
